package com.yowasa.xiuxian.service.helper;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.entity.po.XiuxianBattleField;
import com.yowasa.xiuxian.mapper.XiuxianBattleFieldMapper;
import com.yowasa.xiuxian.mapper.XiuxianExtendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.yowasa.xiuxian.util.ContentUtil.getIndex;

@Component
public class BattleFieldHelper {

    @Autowired
    private XiuxianBattleFieldMapper xiuxianBattleFieldMapper;

    @Autowired
    private XiuxianExtendMapper xiuxianExtendMapper;


    public void insertOrUpdateDamage(UserBO firstUser, UserBO secondUser, int i, XiuXianExtend byAccountId) throws NoSuchFieldException, IllegalAccessException {
        String phase = xiuxianBattleFieldMapper.getPhase();
        //查询本期本层的记录
        XiuxianBattleField battleField = xiuxianBattleFieldMapper.selectOne(
                new LambdaQueryWrapper<XiuxianBattleField>()
                        .eq(XiuxianBattleField::getAccountId, firstUser.getId())
                        .eq(XiuxianBattleField::getPhase, phase));
        String userId = secondUser.getUserId();
        String keyWord = "integral"+userId;
        Class<XiuxianBattleField> xiuxianBattleFieldClass = XiuxianBattleField.class;
        Field declaredField = xiuxianBattleFieldClass.getDeclaredField(keyWord);
        declaredField.setAccessible(true);

        if(ObjectUtil.isNull(battleField)){
            //本期没记录，生成一个
            XiuxianBattleField field = new XiuxianBattleField();
            field.setAccountId(firstUser.getId());
            field.setCreateTime(new Date());
            field.setIntegral1(0);
            field.setIntegral2(0);
            field.setIntegral3(0);
            field.setIntegral4(0);
            field.setPhase(phase);
            if(byAccountId.getBattleField() == 0){

                Integer stage = firstUser.getStage() - 2;
                field.setRank(stage.longValue());
                byAccountId.setBattleField(stage);
                xiuxianExtendMapper.updateById(byAccountId);

            }else {
                field.setRank(byAccountId.getBattleField().longValue());
            }
            declaredField.set(field,i);

            xiuxianBattleFieldMapper.insert(field);

        }else {
            //当前对该怪物造成的伤害
            Integer damage = (Integer) declaredField.get(battleField);

            if(damage < i){
                //本次伤害高，替换掉旧值
                declaredField.set(battleField,i);
            }
            //更新
            xiuxianBattleFieldMapper.updateById(battleField);
        }

    }

    public String rankList(Integer fieldNum) {

        String phase = xiuxianBattleFieldMapper.getPhase();

        List<XiuxianBattleField> xiuxianBattleFields = xiuxianBattleFieldMapper.list(phase,fieldNum);
        StringBuffer sb = new StringBuffer();
        int i = xiuxianBattleFields.size() / 4;

        List<XiuxianBattleField> first = new ArrayList<>();
        List<XiuxianBattleField> second = new ArrayList<>();
        List<XiuxianBattleField> third = new ArrayList<>();

        xiuxianBattleFields.forEach(getIndex((e, index) -> {
            if(i == 0){
                //不足四个人，第一升级其他人保级
                if(index == 0){
                    first.add(e);
                }else {
                    second.add(e);
                }
            }else {
                //四人及以上，前i个升级，后i个降级，中间保级
                if(index < i){
                    first.add(e);
                }else if(index >= xiuxianBattleFields.size() - i){
                    third.add(e);
                }else {
                    second.add(e);
                }
            }
        }));

        sb.append("升级区：\n");
        first.forEach(e -> {
            sb.append(e.getMore1()).append("      ").append(e.getPoint()).append("\n");
        });

        sb.append("保级区：\n");
        second.forEach(e -> {
            sb.append(e.getMore1()).append("      ").append(e.getPoint()).append("\n");
        });

        sb.append("降级区：\n");
        third.forEach(e -> {
            sb.append(e.getMore1()).append("      ").append(e.getPoint()).append("\n");
        });

        return sb.toString();
    }


    public String showMe(UserBO firstUser, XiuXianExtend byAccountId) {
        String phase = xiuxianBattleFieldMapper.getPhase();

        StringBuffer sb = new StringBuffer();
        List<XiuxianBattleField> list = xiuxianBattleFieldMapper.list(phase,byAccountId.getBattleField());
        int i = list.size() / 4;

        list.forEach(getIndex((e, index) -> {
            if(e.getAccountId() == firstUser.getId()){
                if(i == 0){
                    if(index == 0){
                        sb.append("你当前处于升级区");
                    }else {
                        sb.append("你当前处于保级区");
                    }
                }else {
                    if(index < i){
                        sb.append("你当前处于升级区");
                    }else if(index >= list.size() - i){
                        sb.append("你当前处于降级区");
                    }else {
                        sb.append("你当前处于保级区");
                    }
                }
                sb.append("\n").append("总积分为：").append(e.getPoint())
                        .append("\n").append("魔偶剑鬼：").append(e.getIntegral1())
                        .append("\n").append("遗迹巨蛇：").append(e.getIntegral2())
                        .append("\n").append("黯色空壳：").append(e.getIntegral3())
                        .append("\n").append("洛蒂娅：").append(e.getIntegral4());
            }
        }));
        return sb.toString();
    }
}
